package com.my.xinyidaixi.Activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.my.xinyidaixi.Adapter.ListViewSlideAdapter;
import com.my.xinyidaixi.DataBase.Bean.Address;
import com.my.xinyidaixi.DataBase.Model.Over_UserModer;
import com.my.xinyidaixi.DataBase.Model.UserModel;
import com.my.xinyidaixi.Overwrite.SlideListView;
import com.my.xinyidaixi.R;

import java.util.ArrayList;

/**
 * Created by zw on 2017/8/8.
 */
public class add_address extends Activity {
    private SlideListView listView;
    private ArrayList<Address> list=new ArrayList<>();
    private ListViewSlideAdapter listViewSlideAdapter;
    private ImageView fanhui;
    private TextView addbutton;

    private Address item;
    private UserModel model=new Over_UserModer();

    private boolean flag=true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_address_item);
        getData();
        initdata();
    }

    public void initdata() {

        Intent intent=getIntent();

        if(intent.getBooleanExtra("boolean",false))
        {
           flag=false;
        }
        else
        {
            flag=true;
        }
        fanhui = (ImageView) findViewById(R.id.add_address_item_fanhui);

        fanhui.setOnClickListener(new Button());

        addbutton = (TextView) findViewById(R.id.add_address_addbutton);

        addbutton.setOnClickListener(new Button());

        //地址列表初始化并重写 编辑和删除的功能
        listView = (SlideListView) findViewById(R.id.add_address_list);

        listViewSlideAdapter = new ListViewSlideAdapter(this, list,flag);

        listView.setAdapter(listViewSlideAdapter);

        listViewSlideAdapter.setOnClickListenerEditOrDelete(new ListViewSlideAdapter.OnClickListenerEditOrDelete() {
            @Override
            public void OnClickListenerEdit(int position) {

                Intent intent=new Intent();
                Bundle bundle=new Bundle();
                bundle.putSerializable("address",list.get(position));
                intent.putExtras(bundle);
                intent.setClass(add_address.this,tianjia_activity.class);
                startActivityForResult(intent,1);
                //Toast.makeText(add_address.this, "edit position: " + position, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void OnClickListenerDelete(int position)
            {
                //Toast.makeText(add_address.this, "delete position: " + position, Toast.LENGTH_SHORT).show();
                model.delete_address(list.get(position).getId());
                list.remove(position);
                listViewSlideAdapter.notifyDataSetChanged();
            }

            @Override
            public void OnClickListenerRadioButton(int position) {
                if(flag)
                {
                    Intent intent=new Intent();
                    Bundle bundle=new Bundle();
                    bundle.putSerializable("address",list.get(position));
                    intent.putExtras(bundle);
                    add_address.this.setResult(1,intent);
                    add_address.this.finish();
                    overridePendingTransition(0,R.anim.ani_left_sign_out);

                }

            }
        });
    }
//地址列表初始化并重写 编辑和删除的功能


    public class Button implements View.OnClickListener
    {

        @Override
        public void onClick(View v) {
            switch (v.getId())
            {
                case R.id.add_address_item_fanhui:
                add_address.this.setResult(3);
                overridePendingTransition(0,R.anim.ani_left_sign_out);
                add_address.this.finish();
                break;
                case R.id.add_address_addbutton:
                    Intent intent=new Intent();
                    intent.setClass(add_address.this,tianjia_activity.class);
                    startActivityForResult(intent,2);
                    overridePendingTransition(0,R.anim.ani_left_sign_out);
                    break;
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      switch (resultCode)
      {
          case 0:
              break;
          case 1:
              onCreate(null); //重新刷新 利用这种方法是建立在此activity继承了activity示例如果继承的是AppCompatActivity就不行
              break;
          case 2:
              onCreate(null);
              break;
      }
    }

    @Override
    protected void onPause()
    {
        super.onPause();
        add_address.this.overridePendingTransition(R.anim.ani_right_get_into,R.anim.ani_left_sign_out);
    }

    //初试化 地址列表的数据
    private void getData() {

        list=model.get_address();
       /* for(int i=0;i<addressitem.size();i++)
        {
            item = addressitem.get(i);
            address_item=new AddressItem(item.getName(),item.getDizhi_telphonenum(),item.getAddress(),item.getSex(),item.getSchool());
            list.add(address_item);
        }*/

    }
}
